package com.ubtechinc.cruzr.launcher2.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.ubtechinc.cruzr.launcher2.adapter.NestAdapter;
import com.ubtechinc.cruzr.launcher2.utlis.LogUtils;

/**
 * 实现横向翻页效果的RecyclerView
 *
 * @author :ruiqiang
 */
public class PageRecycleView extends RecyclerView {
    private int offsetX = 0;
    //目标页面
    private int mCurrentPage = 0;
    private int mLastPage = 0;
    private int mVelocityX;
    private int mDistanceX = 0;
    private int mLastX;
    private NestAdapter mAdapter;

    public PageRecycleView(Context context) {
        super(context);
    }

    public PageRecycleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public PageRecycleView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void setAdapter(Adapter adapter) {
        mAdapter = (NestAdapter) adapter;
        super.setAdapter(adapter);
    }

    @Override
    public void onScrollStateChanged(int state) {
        super.onScrollStateChanged(state);
        if (state == SCROLL_STATE_IDLE) {
            LogUtils.e("onScrollStateChanged mVelocityX   " + mVelocityX);
            syncPage();
            snopPage();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN && getLayoutManager() != null) {
            mVelocityX = 0;
            mDistanceX = 0;
            mLastX = (int) ev.getX();
            LogUtils.e("ontouch ACTION_DOWN mLastX " + mLastX + " offsetX " + offsetX);
        }
        return super.dispatchTouchEvent(ev);
    }

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public boolean onTouchEvent(android.view.MotionEvent event) {
        switch (event.getAction() & event.getActionMasked()) {
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                mDistanceX = (int) (event.getX() - mLastX);
                LogUtils.i("onTouchEvent mDistanceX = " + mDistanceX + " offsetX " + offsetX);
                break;
            default:
                break;
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean fling(int velocityX, int velocityY) {
        mVelocityX = velocityX;
        return super.fling(0, 0);
    }

    @Override
    public void onScrolled(int dx, int dy) {
        super.onScrolled(dx, dy);
        offsetX += dx;
        LogUtils.i("dx =" + dx + "  offsetX  = " + offsetX);
    }

    /**
     * 根据目标页和偏移量，计划出滚动距离，实现翻页效果
     *
     * @params :
     * @author :ruiqiang
     */
    public void snopPage() {
        if (mLastPage != mCurrentPage) {
            if (mOnPageChangeListener != null) {
                mOnPageChangeListener.onPageChange(mCurrentPage, mAdapter.getPageCount());
            }
            mLastPage = mCurrentPage;
        }
        int xx = mCurrentPage * getWidth() - offsetX;
        LogUtils.i("mCurrentPage = " + mCurrentPage + "   getWidth() = " + getWidth() + "  rqh xx = " + xx);
        if (xx != 0) {
            smoothScrollBy(xx, 0);
        }
    }

    public void snopPageQuick() {
        if (mLastPage != mCurrentPage) {
            if (mOnPageChangeListener != null) {
                mOnPageChangeListener.onPageChange(mCurrentPage, mAdapter.getPageCount());
            }
            mLastPage = mCurrentPage;
        }
        int xx = mCurrentPage * getWidth() - offsetX;
        LogUtils.e("rqh xx = " + xx);
        if (xx != 0) {
            scrollBy(xx, 0);
        }
    }

    /**
     * 根据滑动的距离和速度计算出翻和哪页
     *
     * @params :
     * @author :ruiqiang
     */
    private void syncPage() {
        int pagesize = mAdapter == null ? 0 : mAdapter.getPageCount() - 1;
        if (Math.abs(mVelocityX) <= 1000) {
            if (mDistanceX > 200) {
                mCurrentPage--;
                LogUtils.i("a  mCurrentPage = " + mCurrentPage);
                if (mCurrentPage < 0) {
                    mCurrentPage = 0;
                }
            } else if (mDistanceX < -200) {
                mCurrentPage++;
                LogUtils.i("b  mCurrentPage = " + mCurrentPage);
                if (mCurrentPage > pagesize) {
                    mCurrentPage = pagesize;
                }
            }
        } else {
            if (mVelocityX > 1000) {
                mCurrentPage++;
                LogUtils.i("c  mCurrentPage = " + mCurrentPage);
                if (mCurrentPage > pagesize) {
                    mCurrentPage = pagesize;
                }
            } else if (mVelocityX < -1000) {
                mCurrentPage--;
                LogUtils.i("d  mCurrentPage = " + mCurrentPage);
                if (mCurrentPage < 0) {
                    mCurrentPage = 0;
                }
            }
        }
        mDistanceX = 0;
        mVelocityX = 0;
    }

    public void setCurrentPage(int page) {
        mCurrentPage = page;
    }

    public int getPageIndex() {
        return mCurrentPage;
    }

    onPageChangeListener mOnPageChangeListener;

    public void setOnPageChangeListener(onPageChangeListener listener) {
        mOnPageChangeListener = listener;
    }

    public interface onPageChangeListener {
        void onPageChange(int index, int totalpage);
    }
}
